<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" >
      <el-form-item label="园区" prop="area">
        <el-select v-model="queryParams.area" placeholder="请选择园区"  style="width: 240px" @change="handleQuery">
          <el-option label="全部" value="" ></el-option>
          <el-option v-for="item in dict.type.user_area" :label="item.label" :value="item.value" ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="姓名" prop="name" v-show="showSearch">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入姓名"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item v-show="showSearch">
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-form-item>
      <!--      <el-form-item >-->
      <!--        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport">导出</el-button>-->
      <!--      </el-form-item>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-form>
    <el-table v-loading="loading" :data="rowList">
      <el-table-column label="编号" prop="id" width="120" align="center"/>
      <el-table-column label="园区" prop="area" width="120" align="center"/>
      <el-table-column label="姓名" prop="name" width="120" align="center"/>
      <el-table-column label="性别" prop="gender" width="120" align="center"/>
      <el-table-column label="年龄" prop="age" width="120" align="center"/>
      <el-table-column label="身份证" prop="credit" width="180" align="center"/>
      <el-table-column label="证件号" prop="code" width="180" align="center"/>
      <el-table-column label="体检日期" prop="examine" width="120" align="center">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.examine, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="图片" prop="images" align="center" :width="imageColWidth">
        <template v-slot:default="scope">
          <image-preview v-for="(item,index) in scope.row.images"
                    style="width: 100px; height: 100px; margin-right: 15px"
                    :src="item">
          </image-preview>
        </template>
      </el-table-column>
      <el-table-column label="创建人" prop="createBy" width="120" align="center"/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新人" prop="updateBy" width="120" align="center"/>
      <el-table-column label="修改时间" align="center" prop="createTime" width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="150">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body @close="cancel">
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="姓名" prop="name">
              <el-input v-model="form.name" placeholder="请输入姓名" />
            </el-form-item>
            <el-form-item label="园区" prop="area">
              <el-select v-model="form.area"  clearable placeholder="请选择所在小区">
                <el-option label="二区" value="二区"></el-option>
                <el-option label="一区" value="一区"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="身份证" prop="credit">
              <el-input v-model="form.credit" placeholder="请输入身份证" />
            </el-form-item>
            <el-form-item label="检查时间" prop="examine">
              <el-date-picker v-model="form.examine" format="yyyy-MM-dd" value-format="yyyy-MM-dd"
                              :style="{width: '100%'}" placeholder="选择日期" clearable></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="性别" prop="gender">
              <el-select v-model="form.gender"  clearable placeholder="请选择性别">
                <el-option label="男" value="男"></el-option>
                <el-option label="女" value="女"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="年龄" prop="age">
              <el-input v-model="form.age" placeholder="请输入年龄" />
            </el-form-item>
            <el-form-item label="证件号" prop="code">
              <el-input v-model="form.code" placeholder="请输入证件号" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="上传图片" prop="images" >
          <ImageUpload
            v-model="form.images"
            :limit="8"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { list, get, del, add, update } from "@/api/store/healthy";
import {blurRule, changeRule} from "@/utils/formRules";

export default {
  name: "Healthy",
  dicts: ["user_area"],
  data() {
    return {
      baseUrl: process.env.VUE_APP_BASE_API,
      // 遮罩层
      loading: true,
      imageColWidth: 150,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      rowList: [],
      // 弹出层标题
      title: "",
      open: false,
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined,
        area:'',
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name:blurRule('请输入姓名'),
        area:changeRule('请选择园区'),
        gender:changeRule('请选择性别')
      }
    };
  },
  created() {
    this.queryParams.area=this.$store.state.user.userInfo.area || ''
    this.getList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      list(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          let maxWith = 1;
          for (var i = 0; i < response.rows.length; i++) {
            response.rows[i].imageUrl = response.rows[i].images;
            response.rows[i].images = [];
            if (response.rows[i].imageUrl != null && response.rows[i].imageUrl != undefined) {
              response.rows[i].images = response.rows[i].imageUrl.split(",");
            }
            if (response.rows[i].images.length > maxWith) {
              maxWith = response.rows[i].images.length;
            }
          }
          this.imageColWidth = maxWith * 150;
          this.rowList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.resetForm("form");
      this.form={}
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id;
      get(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id ) {
            // this.form.images = this.form.imageUrl;
            update(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            // this.form.images = this.form.imageUrl;
            add(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const name = row.name;
      this.$modal.confirm('是否确认删除名称为"' + name + '"的数据？').then(function() {
        return del(row.id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    }
  }
};
</script>
